package com.ycsj.goldmedalnewconcept.parent.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.base.OnloadingMoreListener;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.parent.adapter.ScoreRvAdapter;
import com.ycsj.goldmedalnewconcept.parent.bean.ScoreInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 成绩查询
 * Created by Administrator on 2018/6/20 0020.
 */

public class ParentsScoreInquiryActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener, OnloadingMoreListener {

    private ImageView ivBack;
    private TextView tvTitle;
    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private RelativeLayout rlLoadingView;
    //界面刷新的标识
    public static final int UPDATE = 0x881;
    //数据集合
    private List<ScoreInfo.DataBean> oldDatas;

    //适配器
    private ScoreRvAdapter adapter;

    //空视图
    private View emptyView;

    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_score_inquiry;
    }

    @Override
    protected void init() {


        initStateBar();

        //初始化视图
        initView();

        //初始化下拉刷新控件
        initSrf();

        //设置数据
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }


    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("成 绩 查 询");
        rlLoadingView = findViewById(R.id.rl_loading);
        rlLoadingView.setVisibility(View.VISIBLE);
    }


    /**
     * 初始化下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        TextView tvMsg = emptyView.findViewById(R.id.tv_content);
        tvMsg.setText("老师还未上传考试成绩,下次再来吧");
        adapter = new ScoreRvAdapter(this, R.layout.parent_rv_score_inquiry, oldDatas);
    }

    /**
     * 请求服务器数据的方法
     *
     * @param method
     */
    private void http(final int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "7");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("apptoken", AccountUtils.getToken(this));
        map.put("platform", "2");

        HttpUtils.post(HttpAddress.SCOREINQUIRY, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(response)) {
                    ScoreInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ScoreInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                        UiUtils.Toast("数据解析异常", false);
                    }
                    Log.i("test", "成绩详情数据：" + response);
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            List<ScoreInfo.DataBean> newDatas = info.getData();
                            if (method == BqaManager.NORML) {
                                if (null != newDatas && newDatas.size() != 0) {
                                    oldDatas.clear();
                                    oldDatas.addAll(newDatas);
                                }
                                BqaManager.setRv(emptyView, ParentsScoreInquiryActivity.this, adapter, rv);
                            } else {
                                //更新数据
                                BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                adapter.setEnableLoadMore(false);
                            }
                        } else {
                            //返回失败
                            Log.i("test", "服务器返回异常码是:" + info.getState());
                            UiUtils.Toast("服务器返回异常,异常码是:" + info.getState(), false);
                        }
                    }
                }
            }
        });
    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE);
    }

    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }

    /**
     * 事件
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {

        //更新界面的消息
        if (null != busInfo && busInfo.type == UPDATE) {
            http(BqaManager.PULLREFRESH);
        }
    }
}
